package com.fourth.shopping.service.impl;

import com.fourth.entity.Item;
import com.fourth.shopping.commons.TableData;
import com.fourth.shopping.dao.GoodsMapper;
import com.fourth.shopping.dao.ItemMapper;
import com.fourth.shopping.service.ItemService;
import com.fourth.vo.PageCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;
    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public List<Item> getAllItems() {
        return itemMapper.getAllItems();
    }

    @Override
    public TableData<Item> getItemsAsPage(PageCondition condition) {
        List<Item> items = itemMapper.getItemAsPage(condition);
        return new TableData<Item>(items,itemMapper.getAllItemsSize());
    }

    @Override
    public Boolean addNewItem(Item item) {
        if(item.getName() == null || item.getName().equals("")){
            return false;
        }
        if(item.getAlias() == null || item.getAlias().equals("")){
            return false;
        }
        itemMapper.addNewItem(item);
        return true;
    }

    @Override
    public Boolean deleteItem(Integer id) {
        Item item = new Item();
        item.setId(id);
        if(goodsMapper.getGoodsCountByItem(item) > 0){
            return false;
        }
        itemMapper.deleteItem(id);
        return true;
    }

    @Override
    public void modifyItem(Item item) {
        if(item == null){
            return;
        }
        itemMapper.modifyItem(item);
    }

    @Override
    public Item getItemById(String id) {
        return itemMapper.getItemById(id);
    }
}
